<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class ManageController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
    /**
     * 所属组修改方法
     */
    public function group_update(){
    	$id = I('admin_id');
    	$admin = M('Admin')
    	->alias('Ad')
    	->join('__AUTH_GROUP_ACCESS__ AS Ga on Ad.admin_id = Ga.uid','LEFT')
    	->join('__AUTH_GROUP__ AS Gg on Ga.group_id = Gg.id','LEFT')
    	->where(array('admin_id'=>$id))
    	->find();
    	$group = M('Auth_group')->select();
    	$this->assign('admin',$admin);
    	$this->assign('group',$group);
    	$this->display();
    }
    /**
     * 组插入方法
     */
    
    public function group_insert(){
    	$id = I('post.admin_id');
    	$juese = I('post.juese');
    	if(!M('Auth_group_access')->where(array('uid'=>$id))->find()){
    		$data = array(
    			'uid'=>$id,
    			'group_id'=>$juese,
    		);
    		$res = M('Auth_group_access')->add($data);
    	}else{
	    	$res = M('Auth_group_access')->where(array('uid'=>$id))->setField('group_id',$juese);
    	}
    	if(!$res){
    		$this->error('失败',U('admin_index'));
    	}else{
    		$this->error('成功',U('admin_index'));
    	}
    }
    /**
     * 角色列表
     */
    public function juese_index(){
    	$res = M('Auth_group')->select();
    	$this->assign('res',$res);
    	$this->display();
    }
    public function add_juese(){
    	if(IS_POST){
    		$title = I('title');
    		$data = array(
    			'title'=>$title,
    			'status' => 1,
    		);
    		$r = M('Auth_group')->add($data);
    		if(!$r){
    			$this->error('失败',U('juese_index'));
    		}else{
    			$this->error('成功',U('juese_index'));
    		}
    	}else{
	    	$this->display();
    	}
    }
    
    /**
     * 显示权限
     */
    public function showNav(){
    	$id = I('id');
    	$res_group = M('Auth_group')->field('rules')->find($id);
    	$res_grand_father = M('Auth_rule')->where(array('pid'=>0))->select();
    	foreach($res_grand_father as $k=>&$v){
    		$v['father'] = M('Auth_rule')->where(array('pid'=>$v['id']))->select();
    		foreach($v['father'] as $s=>&$x){
    			$x['child'] = M('Auth_rule')->where(array('pid'=>$x['id']))->select();
    		}
    	}
    	$this->assign('rules',$res_group['rules']);
    	$this->assign('rules_total',$res_grand_father);
//     	dump($res_grand_father);die;
    	$this->assign('id',$id);
    	$this->display();
    }
    /**
     * 修改权限
     */
    public function saveNav() {
    	$id = I('post.id');
    	$arr = I('post.rules');
    	$str = implode(',',$arr);
    	$res = M('Auth_group')->where(array('id'=>$id))->setField('rules',$str);
    	if(!$res){
    		$this->error('修改失败');
    	}else{
    		$this->success('修改成功',U('Manage/juese_index'));
    	}
    }
    /**
	 * 管理员列表
	 */
    public function admin_index(){
		//$admin = D('AdminView')->select();
		$admin = M('Admin')
		->alias('Ad')
		->join('__AUTH_GROUP_ACCESS__ AS Ga on Ad.admin_id = Ga.uid','LEFT')
		->join('__AUTH_GROUP__ AS Gg on Ga.group_id = Gg.id','LEFT')
		->select();
    	$this->assign('admin',$admin);
    	$this->display();
    }
    /**
     * 添加管理员
     */
    public function addAdmin(){
        $admin=M('Admin');
        if(IS_POST){
            $username=I('post.username');

            $pwd=md5(I('post.password'));
            if(empty($username)||empty($pwd)){
                $this->error('请补全信息');
            }
            $data['username']=$username;
            $data['password']=$pwd;
            $data['pwd_show']=I('post.password');
            if(empty($_POST['admin_id'])){
                $rs=$admin->where("username='$username'")->find();
                if($rs){
                    $this->error('用户名称已存在');
                }
                $rs=$admin->add($data);
            }else{
                $data['admin_id']=I('post.admin_id');
                if(empty($pwd)){
                    $data['password']=md5(I('post.pwd_show'));
                    $data['pwd_show']=I('post.pwd_show');
                }
                $rs=$admin->save($data);
            }
            if($rs){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }
        if(!empty($_GET['admin_id'])){
            $list=$admin->where('admin_id='.$_GET['admin_id'])->find();
        }
        $this->assign('list',$list);
        $this->display();
    }
    /**
     * 修改本账号密码
     */
    public function pwdUpdate(){
        header("Content-type:text/html;charset=utf-8");
        $admin_id = session('admin_userid');
        $list=M('Admin')->where('admin_id='.$admin_id)->find();
        if(empty($admin_id)){
            $this->error('操作有误');
            return;
        }
        if(IS_POST){
            if(empty($_POST['old_pwd'])){
                $this->error('请输入原始密码');
                return;
            }
            if(empty($_POST['password'])){
                $this->error('请输入新密码');
                return;
            }
            $old_pwd = md5($_POST['old_pwd']);
            if($list['password'] != $old_pwd){
                $this->error('您输入的原始密码错误');
                return;
            }
            if(!checkPwd($_POST['password'])){
                $this->error('验证密码长度在6-20个字符之间');
                return;
            }
            $r = M('Admin')->where('admin_id='.$admin_id)->setField('password',md5($_POST['password']));
            if($r===true){
                $this->error('修改失败');
                return;
            }else{
                $this->redirect('Login/loginout','',1,"<script>alert('修改成功请重新登录')</script>");
                return;
            }
        }else{
            $this->assign('list',$list);
            $this->display();
        }
    }
    /**
     * 删除管理员
     */
    public function delMember(){
        $this->auth();
        if(empty($_GET['admin_id'])){
            $this->error('要删除的ID不存在');
        }
        if($_GET['admin_id']==1){
            $this->error('此ID不可删除');
        }
        $admin = M('Admin');
        $r = $admin->delete($_GET['admin_id']);
        if($r){
            $this->success('删除成功',U('Manage/index'));
            return;
        }else{
            $this->error('删除失败');
            return;
        }
    }
}